package org.liuhy.node.focus
{
	import flash.events.MouseEvent;
	
	import mx.containers.Canvas;
	import mx.core.DragSource;
	import mx.core.UIComponent;
	import mx.events.DragEvent;
	import mx.managers.DragManager;
	import mx.utils.ObjectUtil;
	
	import org.CacheUtils;
	import org.liuhy.FlowPanel;
	import org.liuhy.line.AbstractLine;
	import org.liuhy.line.ArrowLineNode;
	import org.liuhy.line.CLine;
	import org.liuhy.line.FromLineNode;
	import org.liuhy.line.LineNode;
	import org.liuhy.node.FlowNode;
	
	public class FocusNode extends Canvas
	{				
		public var _ADJUST_LEN:int = 15;
		public function FocusNode(p_x:int,p_y:int)
		{
			this.x = p_x;
			this.y = p_y;			
			this.width = 10;
			this.height = 10;
			this.addEventListener(DragEvent.DRAG_ENTER,onDragEnter);
			this.addEventListener(DragEvent.DRAG_OVER,onDragOver);			
			this.addEventListener(DragEvent.DRAG_DROP,lineNodeDrop);
			this.addEventListener(DragEvent.DRAG_EXIT,onDragExit);
			this.addEventListener(MouseEvent.MOUSE_DOWN,onClick);
			this.addEventListener(MouseEvent.MOUSE_MOVE,onMouseMove);
			
			this.graphics.lineStyle(1, 0x000000);
			this.graphics.moveTo(this.width/2-2,this.height/2-2);
			this.graphics.lineTo(this.width/2+2,this.height/2+2);			
			this.graphics.moveTo(this.width/2-2,this.height/2+2);
			this.graphics.lineTo(this.width/2+2,this.height/2-2);
		}
		public function setOnFocusNode():void{
			this.setStyle("borderColor",0xFF0000);
			this.setStyle("backgroundAlpha","1");
			this.setStyle("backgroundColor",0xFF0000);
		}
		public function setOutFocusNode():void{
			this.setStyle("borderColor",0xFFFFFF);
			this.setStyle("backgroundAlpha","0");
			this.setStyle("backgroundColor",0x000000);
		}
		//调整流程节点大小;
		public function adjustFlowNode(event:DragEvent):void{
			
		}
		private function onClick(event:MouseEvent):void{
			event.stopPropagation();
		}
		private function onMouseMove(event:MouseEvent):void{
			event.stopPropagation();
			var dragInitiator:FocusNode = FocusNode(event.currentTarget);
            var ds:DragSource = new DragSource();
            ds.addData(this,"focusNode");
            DragManager.doDrag(dragInitiator, ds, event);
		}
		public function get _x():int{
			return this.x + 5;
		}
		public function get _y():int{
			return this.y + 5
		}
		public function get pos():String{
			return null;
		}
		public function onDragEnter(event:DragEvent):void{
			if((event.dragInitiator is FromLineNode) || (event.dragInitiator is ArrowLineNode)){
//				this.setStyle("backgroundColor","#FFCCEE");						
				DragManager.acceptDragDrop(UIComponent(event.currentTarget));
				this.setOnFocusNode();				
			}	
		}
		public function onDragOver(event:DragEvent):void{
			if(event.dragInitiator is LineNode){
				var _lineNode:LineNode = event.dragInitiator as LineNode;
				_lineNode.lineNodeFocusVirtualDrag(event);
				this.setOnFocusNode();		
			}
		}
		public function onDragExit(event:DragEvent):void{			
			this.setStyle("backgroundColor","#ffffff");
			this.setOutFocusNode();
		}
		//拖动流程节点后,调整关联线;
		public function adjustULine(p_uLine:AbstractLine):void{
			var _cLine:CLine = p_uLine.parent as CLine;
			if(this==p_uLine.in_flowFocusNode){
				//调整箭头;
				p_uLine.focusNodeAdjustArrowLine(this);
			}
			if(this==p_uLine.out_flowFocusNode){
				//调整开始节点;
				p_uLine.focusNodeAdjustFromLine(this);
			}
			_cLine.adjustTitle(null);
		}
		public function adjustArrowULine(p_uLine:AbstractLine):void{
			
		}
		public function adjustFromULine(p_uLine:AbstractLine):void{
			
		}
		public function lineNodeDrop(event:DragEvent):void{
			//CLONE;
			var _panel:FlowPanel = this.parent.parent as FlowPanel;
			CacheUtils.push(_panel.exportXML());
			//
			var _flowNode:FlowNode = this.parent as FlowNode;
			//流程节点选择器;
			var _uLine:AbstractLine = event.dragSource.dataForFormat("line") as AbstractLine;
			var _cLine:CLine = _uLine.parent as CLine;
			var _moveNode:LineNode = event.dragInitiator as LineNode;
			//线头
			if(null==_uLine.preLine && _moveNode is FromLineNode &&_flowNode.isPermitFromDrop()){
				_uLine.focusNodeDropFromLine(this);
			}
			//箭头
			if(null==_uLine.nextLine && _moveNode is ArrowLineNode &&_flowNode.isPermitAllowDrop()){
				_uLine.focusNodeDropArrowLine(this);
			}
			//设置流程节点显示在面板最前面;
			var _canvas:Canvas = _flowNode.parent as Canvas;
			_canvas.setChildIndex(_flowNode,_canvas.numChildren-1);
			_cLine.adjustTitle(null);
			this.setOutFocusNode();
			
		}
		/**
		 * p_uLine 线段;
		 * p_moveNode 拖动的节点;
		 **/
		public function fromLineNodeDrop(p_uLine:AbstractLine,p_moveNode:LineNode):void{
			
		}
		/**
		 * p_uLine 线段;
		 * p_moveNode 拖动的节点;
		 **/
		public function arrowLineNodeDrop(p_uLine:AbstractLine,p_moveNode:LineNode):void{
			
		}
	}
}